package com.weiyuwqj.app.mobile.account;

import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.exception.ExceptionMessage;
import com.weiyuwqj.api.exception.HfException;
import com.weiyuwqj.api.model.BusAccountCash;
import com.weiyuwqj.api.service.BusAccountCashService;
import com.weiyuwqj.api.service.BusAccountService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 提现
 * Created by Administrator on 2016/1/8.
 */
@Controller
@RequestMapping(value = "/mobile/account")
public class MobileAccountCashController {

    @Autowired
    private BusAccountService accountService;

    @Autowired
    private BusAccountCashService accountCashService;

    @Autowired
    private BusInvestService investService;

//    /**
//     * 提现申请
//     *
//     * @param userId 用户ID
//     * @param amount 提现金额
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/cash", method = RequestMethod.POST)
//    public Object cash(BigDecimal amount, Integer userId ,String resource) {
//        //请求提现, 添加记录
//        BusAccountCash cash = accountCashService.doCash(amount, userId,resource);
//        try {
//            //请求恒丰接口
//            accountCashService.doHfWithdraw(cash);
//        } catch (HfException e) {
//            //更新失败原因
//            accountCashService.updateBusAccountCashStatus(cash.getOrderNo(), 2, 0, IpUtils.get());
//            accountCashService.updateRemarkByOrderNo(cash.getOrderNo(), e.getMessageText());
//            throw new HfException("HfException", e.getMessageText());
//        } catch (CrowdException e) {
//            //更新失败原因
//            accountCashService.updateBusAccountCashStatus(cash.getOrderNo(), 2, 0, IpUtils.get());
//            accountCashService.updateRemarkByOrderNo(cash.getOrderNo(), ExceptionMessage.exceptionMessage.get(e.getCode()));
//            throw new CrowdException(e.getCode());
//        }
//        return Message.OK;
//    }

    /**
     * 显示提现页面的数据
     *
     * @param userId 用户ID
     * @return 账户信息
     */
    @ResponseBody
    @RequestMapping(value = "/cashData", method = RequestMethod.POST)
    public Object cashData(Integer userId) {
        return accountService.getBusAccountByUserId(userId);
    }

    //根据userId获取提现列表
    @ResponseBody
    @RequestMapping("/getCashListByUserId")
    public Object getCashListByUserId(Paginator p, Integer userId) {
        return accountCashService.getListByUserId(p, userId);
    }

    //判断是否服务器是否在提现时间
    @ResponseBody
    @RequestMapping("/getServerTime")
    public Object getServerTime() {
        Date now = investService.getServerTime();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String thisDayTime = sdf.format(now);
        try {
            Long millisecond = sdf.parse(thisDayTime).getTime();
            Long endMillisecond = sdf.parse("23:30:00").getTime();
            Long beginMillisecond = sdf.parse("00:05:00").getTime();
            if (millisecond > endMillisecond || millisecond < beginMillisecond) {
                throw new CrowdException("user.cash.not_correct_time");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return Message.FAIL;
        }
        return Message.OK;
    }

}
